package cloud.hadoop.bds.utils;

import cloud.hadoop.bds.model.common.Result;
import cloud.hadoop.bds.pojo.Course;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReadFile {
    public Result readCourseData(String filePath) {
        // 读取文件
        // return Result.success("文件读取完成, 文件路径: " + filePath, IoUtils.readLines(filePath));
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            List<Course> output = new ArrayList<>();
            // 字符缓冲流读取
            reader.lines().forEach(line -> {
                final String[] arr = line.split("\\t");
                output.add(Course.builder()
                        .school("岭南师范学院")
                        .name(arr[0])
                        .major("计算机科学与技术")
                        .value(arr[1])
                        .grade("18级")
                        .build());
            });
            return Result.success("文件读取完成", output);
        } catch (IOException e) {
            return Result.business("文件读取错误", Collections.emptyList());
        }
    }
}
